package com.guxingke.typecho4j.widget.handle.comment;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.Comment;
import com.guxingke.typecho4j.jfinal.model.Content;
import com.guxingke.typecho4j.route.Route;
import com.guxingke.typecho4j.route.Routes;
import com.guxingke.typecho4j.util.DateUtil;
import com.guxingke.typecho4j.util.RegexpCheckUtils;
import com.guxingke.typecho4j.widget.WidgetHandle;
import com.guxingke.typecho4j.widget.provider.CommentBaseWidget;
import com.guxingke.typecho4j.widget.provider.ProviderWidget;
import com.guxingke.typecho4j.widget.provider.comment.CommentsWidget;
import com.jfinal.core.Controller;

/**
 * 新增评论处理组件
 * 
 * @author guxingke
 * */
public class CommentHandleWidget extends CommentBaseWidget implements
		WidgetHandle {

	@Override
	public void handle(Controller controller) throws Typecho4jException {
		// cid 可以从returnPath中得到
		// 当前没有用户登录的情况下，authorId=0;
		// created为当前时间的unixstamp
		// ownerId 为当前内容的创建者ID
		// ip,agent,可从http header中取得
		// type= comment,status=approved
		// parent ，评论的父级评论的 coid,暂时只支持二级评论
		String returnPath = getReturnPath(controller);

		Comment temp = controller.getModel(Comment.class);

		// xss过滤
		temp.set("author", RegexpCheckUtils.htmlRemoveTag(temp
				.getStr("author")));
		temp.set("url", RegexpCheckUtils.htmlRemoveTag(temp
				.getStr("url")));
		temp.set("mail", RegexpCheckUtils.htmlRemoveTag(temp
				.getStr("mail")));
		temp.set("text", RegexpCheckUtils.htmlRemoveTag(temp
				.getStr("text")));

		// 获取cid
		Long cid = getCidByReturnPath(returnPath);
		temp.set("cid", cid);

		// 设置ownerId
		Long ownerId = getOwnerId(cid);
		temp.set("ownerId", ownerId);

		// 获取parent
		Long parent = controller.getParaToLong("parent");
		if (parent == null) {
			parent = 0L;
		}
		temp.set("parent", parent);

		// 获取ip
		String ip = getCommentIp(controller);
		temp.set("ip", ip);

		// 获取agent
		String agent = getAgent(controller);
		temp.set("agent", agent);

		// 设置创建时间
		temp.set("created", DateUtil.getUnixTimestamp());

		// 设置authorId,1.用户是否登录，登录则有隐藏域authorId
		Long authorId = controller.getParaToLong("authorId");
		if (authorId == null) {
			authorId = 0L;
		}
		temp.set("authorId", authorId);

		// 设置type
		temp.set("type", "comment");

		// 设置status
		temp.set("status", "approved");

		// 添加评论
		addComment(temp);

		// 刷新comment缓存
		ProviderWidget.refreshProviderWidget(CommentsWidget.class);

		// 重定向到请求发起页
		controller.redirect(returnPath);
	}

	private Long getOwnerId(Long cid) {
		return Content.dao.getUserIdByContentId(cid);
	}

	private String getAgent(Controller controller) {
		return controller.getRequest().getHeader("user-agent");
	}

	private String getCommentIp(Controller controller) {
		return controller.getRequest().getRemoteAddr();
	}

	private Long getCidByReturnPath(String returnPath) {
		Long cid = null;
		String[] temps = returnPath.split("/");
		String result = temps[temps.length - 1];
		try {
			cid = Long.parseLong(result);
		} catch (Exception e) {
			cid = Content.dao.getCidBySlug(result);
		}
		return cid;
	}

	private String getReturnPath(Controller controller) {
		return controller.getRequest().getHeader("referer");
	}

	@Override
	public void configRoute(Routes routes) {
		Route commentRoute = new Route("comment", this.getClass(), null);
		routes.addRoute(commentRoute);
	}
}
